package com.hrd.admin.system.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hrd.admin.base.BaseController;
import com.hrd.admin.base.BaseDefine;
import com.hrd.common.util.bean.ServiceResult;
import com.hrd.mybatis.bean.ext.ExtOrderModel;
import com.hrd.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: wangpeng
 * @Date: 2022/1/14 10:02
 * @Description:
 */
@Controller
@RequestMapping("/system/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("init")
    public ModelAndView init() {
        ModelAndView modelAndView = new ModelAndView("/system/order/order_list");
        return modelAndView;

    }

    @RequestMapping(value = "searchAction")
    public JSONObject searchAction(HttpServletRequest request, @ModelAttribute(BaseDefine.FORM) ExtOrderModel form) {
        // 创建分页
        return this.createPage(request, form);
    }

    /**
     * 创建分页机能
     *
     * @param request
     * @param form
     */
    private JSONObject createPage(HttpServletRequest request, ExtOrderModel form) {

        JSONObject object = new JSONObject();
        // 翻页
        form.setLimitStart(Integer.parseInt(form.getOffset()));
        form.setLimitEnd(Integer.parseInt(form.getOffset())+Integer.parseInt(form.getLimit()));

        ServiceResult<List<ExtOrderModel>> result = orderService.list(form);
        Integer count = orderService.count(form);

        if (result != null) {
            List<ExtOrderModel> recordList = result.getResult();
            PageInfo paginator = new PageInfo(recordList);// 默认8页数据
            object.put("rows", paginator.getList());
            object.put("total", count);
        }

        return object;
    }

    @ResponseBody
    @RequestMapping("updateAction")
    public ServiceResult<ExtOrderModel> updateAcction(HttpServletRequest request, ExtOrderModel form) {
        return orderService.synchOrder(form);
    }

    /**
     * 付款单同步
     * @param request
     * @param form
     * @return
     */
    @ResponseBody
    @RequestMapping("fkOrderSynchAction")
    public ServiceResult<ExtOrderModel> fkOrderSynchAction(HttpServletRequest request, ExtOrderModel form) {
        return orderService.fkOrderSynchAction(form);
    }

    /**
     * 出库单同步
     * @param request
     * @param form
     * @return
     */
    @ResponseBody
    @RequestMapping("ckOrderSynchAction")
    public ServiceResult<ExtOrderModel> ckOrderSynchAction(HttpServletRequest request, ExtOrderModel form) {
        return orderService.ckOrderSynchAction(form);
    }

    /**
     * 入库单同步
     * @param request
     * @param form
     * @return
     */
    @ResponseBody
    @RequestMapping("rkOrderSynchAction")
    public ServiceResult<ExtOrderModel> rkOrderSynchAction(HttpServletRequest request, ExtOrderModel form) {
        return orderService.rkOrderSynchAction(form);
    }

    /**
     * 入库单同步
     * @param request
     * @param form
     * @return
     */
    @ResponseBody
    @RequestMapping("allOrderSynchAction")
    public ServiceResult<ExtOrderModel> allOrderSynchAction(HttpServletRequest request, ExtOrderModel form) {
        return orderService.allOrderSynchAction(form);
    }
}
